<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Common;

use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;
use NRG\Framework\Exceptions\IndexOutOfBoundsException as IndexOutOfBoundsException;

/**
 * Simple static application registry
 *
 * @package NRG\Framework\Common
 */
class Registry
{
    /**
     * Array of registered items
     *
     * @var array
     */
    protected static $_items = array();

    /**
     * Register an item
     *
     * @param string $key
     * @param mixed $item
     */
    public static function put($key, $item=null)
    {
	if(!is_string($key))
	    throw new IllegalArgumentException("Key must be an instance of string");

	$key = trim($key);

	if($key == "")
	    throw new IllegalArgumentException("Key cannot be empty");
	
	self::$_items[$key] = $item;
    }

    /**
     * Retrieve a registry entry by key
     *
     * @param string $key
     * @return mixed
     * @throws IndexOutOfBoundsException
     */
    public static function get($key=null)
    {
	if(!is_string($key))
	    throw new IllegalArgumentException("Key must be an instance of string");

	$key = trim($key);

	if($key == "")
	    throw new IllegalArgumentException("Key cannot be empty");
	
	if(key_exists($key, self::$_items))
	    return self::$_items[$key];
	else
	    throw new IndexOutOfBoundsException("Key does not " .
		    "exist in application registry: " . $key);
    }

    /**
     * Check if a key exists in the registry
     * 
     * @param string $key
     * @return boolean
     */
    public static function hasKey($key)
    {
	if(!is_string($key))
	    throw new IllegalArgumentException("Key must be an instance of string");

	$key = trim($key);

	if($key == "")
	    throw new IllegalArgumentException("Key cannot be empty");

	return array_key_exists($key, self::$_items);
    }
}
